﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CardLib
{
    public class Deck
    {
        private Random random = new Random();
        private List<Card> cards = new List<Card>();
        private Card trumphCard;
        public List<Card> Cards
        {
            get { return cards; }
            set { cards = value; }
        }

        public int NumberOfCards
        {
            //Shortcut property to get the number of cards in the deck

            get { return cards.Count; }
        }

        public Card GetRandomCard()
        {
            // Returns a random card from the deck.  
            // Removes the card from the deck.  
            if (cards.Count == 0)
                return null;
                //throw new Exception("EmptyDeckException");
            int cardId = -1;
            do
            {
                 cardId = random.Next(0, NumberOfCards - 1);
                 Card card1 = cards[cardId];
                 if (trumphCard == null)
                     break;
                 if (card1 != trumphCard)
                     break;
                

            } while (true);

            Card card = cards[cardId];
            this.Cards.Remove(card);

            return card;
        }

        public Card GetTopCard()
        {
            // Gets the first (top) card in the deck
            // Essentially pulling from the top of the deck.
            // Removes the card from the deck

            if (cards.Count == 0)
                throw new Exception("EmptyDeckException");

            Card card = cards[0];
            this.Cards.Remove(card);

            return card;
        }

        public Card GetBottomCard()
        {
            // Gets the last (bottom) card in the deck
            // Essentially pulling from the bottom of the deck.
            // Removes the card from the deck

            if (cards.Count == 0)
                throw new Exception("EmptyDeckException");

            Card card = cards[cards.Count - 1];
            this.Cards.Remove(card);

            return card;
        }

        public Card GetCard(int index)
        {
            // Gets a card in the deck
            // Removes the card from the deck

            if (cards.Count == 0)
                throw new Exception("EmptyDeckException");

            if (cards.Count <= index)
                throw new Exception("InvalidCardException");

            Card card = cards[index];
            this.Cards.Remove(card);

            return card;
        }

        public Card GetCard(CardValues cardValue, CardSuits suit)
        {
            foreach (Card c in this.Cards)
            {
                if (c.Value == cardValue && c.Suit == suit)
                {
                    this.Cards.Remove(c);
                    return c;
                }
            }

            return null;
        }

      

        public bool Exists(Card aCard)
        {
            foreach (Card c in cards)
            {
                if (aCard.TextValue == c.TextValue)
                {
                    return true;
                }
            }

            return false;
        }

        public void setTrumphCard(Card trumphCard)
        {
            this.trumphCard = trumphCard;
        }





    }
}
